// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.standard.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.DesignClause;
import com.liuxinlong.modules.standard.controller.request.QueryDesignClauseReq;
import com.liuxinlong.modules.standard.controller.request.UpdateDesignClauseReq;
import com.liuxinlong.modules.standard.service.DesignClauseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设计条款控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022年2月24日
 */
@RestController
@RequestMapping("/clause/design")
public class DesignClauseController {

    @Autowired
    private DesignClauseService designClauseService;

    /**
     * 设计条款分页查询
     *
     * @param req 查询参数
     * @return 设计条款分页信息
     */
    @PostMapping("page")
//    @LogAnnotation(value = "分页查询", business = LogBusinessConstants.DESIGN_CLAUSE,operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp pageClauseInfo(@RequestBody QueryDesignClauseReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("hierarchy", req.getHierarchy());
        queryMap.put("name", req.getName());
        List<Map<String, Object>> dataList = designClauseService.pageClauseInfo(queryMap);
        int totalNum = designClauseService.getClauseCount(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询设计条款信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 新增设计条款信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("add")
    @LogAnnotation(value = "新增设计条款", business = LogBusinessConstants.DESIGN_CLAUSE,operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addStandardInfo(@RequestBody UpdateDesignClauseReq req) {
        DesignClause clause = new DesignClause();
        clause.setClauseSn(req.getClauseSn());
        clause.setName(req.getName());
        clause.setHierarchy(req.getHierarchy());
        clause.setMandatory(req.getMandatory());
        clause.setApplicability(req.getApplicability());
        clause.setApplicabilityAttach(req.getApplicabilityAttach());
        clause.setReference(req.getReference());
        clause.setReferenceAttach(req.getReferenceAttach());
        clause.setRequirement(req.getRequirement());
        clause.setRequirementAttach(req.getRequirementAttach());
        clause.setAttach(req.getAttach());
        clause.setRemark(req.getRemark());
        designClauseService.addClauseInfo(clause, req.getStandardId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增设计条款信息成功");
        return resp;

    }

    /**
     * 修改设计条款信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update")
    @LogAnnotation(value = "修改设计条款", business = LogBusinessConstants.DESIGN_CLAUSE,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateStandardInfo(@RequestBody UpdateDesignClauseReq req) {
        DesignClause clause = new DesignClause();
        clause.setId(req.getId());
        clause.setClauseSn(req.getClauseSn());
        clause.setName(req.getName());
        clause.setHierarchy(req.getHierarchy());
        clause.setMandatory(req.getMandatory());
        clause.setApplicability(req.getApplicability());
        clause.setApplicabilityAttach(req.getApplicabilityAttach());
        clause.setReference(req.getReference());
        clause.setReferenceAttach(req.getReferenceAttach());
        clause.setRequirement(req.getRequirement());
        clause.setRequirementAttach(req.getRequirementAttach());
        clause.setAttach(req.getAttach());
        clause.setRemark(req.getRemark());
        designClauseService.updateClauseInfo(clause, req.getStandardId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改设计条款信息成功");
        return resp;
    }

    /**
     * 删除设计条款信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value = "删除设计条款", business = LogBusinessConstants.DESIGN_CLAUSE,operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteStandardInfo(@RequestBody BaseDeleteReq req) {
        designClauseService.deleteClauseInfo(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除设计条款信息成功");
        return resp;
    }

    /**
     * 导出设计条款信息
     *
     * @param req 请求信息
     * @return 导出数据
     */
    @PostMapping("export")
    @LogAnnotation(value = "导出设计条款", business = LogBusinessConstants.DESIGN_CLAUSE,operateType = LogOperateConstants.OPERATE_EXPORT)
    public void exportStandardInfo(@RequestBody BaseDeleteReq req, HttpServletResponse response) {
        designClauseService.exportClauseInfo(req.getId(), response);

    }

    /**
     * 查询设计标准下设计条款
     *
     * @param req 查询参数
     * @return 设计条款信息
     */
    @PostMapping("list")
    @LogAnnotation(value = "查询设计标准下设计条款", business = LogBusinessConstants.DESIGN_CLAUSE,operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp listStandardClause(@RequestBody BaseDeleteReq req) {
        List<Map<String, Object>> dataList = designClauseService.listStandardClause(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("查询设计标准下设计条款成功");
        resp.setDataList(dataList);
        return resp;
    }
}
